using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FixedWidthFileViewer
{
    public partial class EditSchemaForm : Form
    {
        FixedWidthSchema schema;
        BindingSource bindingSource;

        public EditSchemaForm(FixedWidthSchema lSchema)
        {
            schema = lSchema;
            InitializeComponent();
        }

        private void EditSchemaForm_Load(object sender, EventArgs e)
        {
            bindSchema();
        }

        private void bindSchema()
        {
            schemaDGV.SuspendLayout();
            bindingSource = new BindingSource();
            bindingSource.DataSource = schema.Fields;
            schemaDGV.DataSource = bindingSource;
            schemaDGV.SelectionMode = DataGridViewSelectionMode.CellSelect;
            schemaDGV.ResumeLayout();
            recLengthLBL.Text = "Record Length: " + schema.GetRecordLength().ToString();
        }

        private void okBTN_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelBTN_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void schemaDGV_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (schemaDGV.RowCount > 0)
                {
                    DataGridView.HitTestInfo hit = schemaDGV.HitTest(e.X, e.Y);
                    schemaDGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    schemaDGV.CurrentCell = schemaDGV.Rows[hit.RowIndex].Cells[0];
                    foreach (ToolStripMenuItem mi in dataGridCSM.Items)
                    {
                        mi.Enabled = true;
                    }
                }
                else
                {
                    foreach (ToolStripMenuItem mi in dataGridCSM.Items)
                    {
                        if (mi.Text == "Add New")
                        {
                            mi.Enabled = true;
                        }
                        else
                        {
                            mi.Enabled = false;
                        }
                    }
                }
            }
        }

        private void moveUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = schemaDGV.CurrentCell.RowIndex;
            if (rowIndex < 1)
            {
                MessageBox.Show("Error.  Cannot move the first row up any further.");
                return;
            }
            FixedWidthField moveFw, otherFw;
            moveFw = schema.Fields[rowIndex];
            otherFw = schema.Fields[rowIndex - 1];
            schema.Fields[rowIndex] = otherFw;
            schema.Fields[rowIndex - 1] = moveFw;
            bindingSource.ResetBindings(false);
        }

        private void moveDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int rowIndex = schemaDGV.CurrentCell.RowIndex;
            if (rowIndex >= schema.Fields.Count - 1)
            {
                MessageBox.Show("Error.  Cannot move the last row down any further.");
                return;
            }
            FixedWidthField moveFw, otherFw;
            moveFw = schema.Fields[rowIndex];
            otherFw = schema.Fields[rowIndex + 1];
            schema.Fields[rowIndex] = otherFw;
            schema.Fields[rowIndex + 1] = moveFw;
            bindingSource.ResetBindings(false);
        }


    }
}